﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApCableLengthUpdater
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.ComponentModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands;

public class CApCableLengthUpdater : IDisposable
{
  private ITxLocatableObject m_previousStartFrame;
  private ITxLocatableObject m_previousEndFrame;
  private bool m_hasActed;
  private readonly ICApFLEXCableAttributeViewModel m_attributeViewModel;

  public CApCableLengthUpdater(ICApFLEXCableAttributeViewModel attributeViewModel)
  {
    this.m_attributeViewModel = attributeViewModel ?? throw new ArgumentNullException(nameof (attributeViewModel));
    this.m_hasActed = this.m_attributeViewModel.IsEditMode;
    this.m_attributeViewModel.PropertyWillChange += new PropertyChangedEventHandler(this.AttributeViewModel_PropertyWillChange);
    this.m_attributeViewModel.PropertyChanged += new PropertyChangedEventHandler(this.AttributeViewModel_PropertyChanged);
  }

  public void Dispose()
  {
    this.m_attributeViewModel.PropertyWillChange -= new PropertyChangedEventHandler(this.AttributeViewModel_PropertyWillChange);
    this.m_attributeViewModel.PropertyChanged -= new PropertyChangedEventHandler(this.AttributeViewModel_PropertyChanged);
  }

  private void UpdateHasActedTrigger()
  {
    this.m_hasActed = this.m_attributeViewModel.IsEditMode || this.m_attributeViewModel.Cable != null || this.m_attributeViewModel.StartFrame != null && this.m_attributeViewModel.EndFrame != null;
  }

  private void OnFrameChanged(
    ITxLocatableObject previousFrame,
    ITxLocatableObject newFrame,
    ITxLocatableObject otherFrame)
  {
    if (this.m_hasActed || previousFrame == newFrame || newFrame == null || otherFrame == null)
      return;
    this.m_hasActed = true;
    double distance;
    if (!CApCableUtility.ComputeDistanceBetweenFrames(newFrame, otherFrame, out distance))
      return;
    distance = CApCableUtility.ComputeLinearRoundValueWithCurrentUnit(distance);
    distance *= TxApplication.Options.Units.LinearMultiplier;
    if (this.m_attributeViewModel.Length >= distance)
      return;
    this.m_attributeViewModel.Length = distance;
  }

  private void AttributeViewModel_PropertyWillChange(object sender, PropertyChangedEventArgs e)
  {
    switch (e.PropertyName)
    {
      case "StartFrame":
        this.m_previousStartFrame = this.m_attributeViewModel.StartFrame;
        break;
      case "EndFrame":
        this.m_previousEndFrame = this.m_attributeViewModel.EndFrame;
        break;
    }
  }

  private void AttributeViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
  {
    switch (e.PropertyName)
    {
      case "StartFrame":
        this.OnFrameChanged(this.m_previousStartFrame, this.m_attributeViewModel.StartFrame, this.m_attributeViewModel.EndFrame);
        break;
      case "EndFrame":
        this.OnFrameChanged(this.m_previousEndFrame, this.m_attributeViewModel.EndFrame, this.m_attributeViewModel.StartFrame);
        break;
      case "IsEditMode":
        this.UpdateHasActedTrigger();
        break;
    }
  }
}
